package com.example.customcomwidget;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * @author hdz
 * @date 2021/08/05 3:57 PM
 */
public class OptionTextAdapter extends BaseAdapter {
    private List<OptionText> data;
    private Context context;
    private int curIndex;

    public OptionTextAdapter(Context context, List<OptionText> data) {
        this.data = data;
        this.context = context;
    }

    public void setCurIndex(int curIndex) {
        this.curIndex = curIndex;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public OptionText getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.option_text_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.item = (LinearLayout) convertView.findViewById(R.id.item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (this.curIndex == position) {
            viewHolder.imageView.setVisibility(View.VISIBLE);
            viewHolder.item.setBackgroundColor(Color.GRAY);
        } else {
            viewHolder.imageView.setVisibility(View.INVISIBLE);
            viewHolder.item.setBackgroundColor(Color.WHITE);
        }
        OptionText optionText = data.get(position);
        viewHolder.textView.setText(optionText.getText());
        return convertView;
    }

    private static class ViewHolder {
        TextView textView;
        ImageView imageView;
        LinearLayout item;
    }
}
